﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreFrame.Business;
using MarchEstate.Manager.BusinesEntity;
using CoreFrame.Core.Orm;
using System.Data;

namespace MarchEstate.Manager.Dao.Room
{
    public class AreaRoomDao : AbstractDao
    {
        /// <summary>
        /// 是否存在该区域对象
        /// </summary>
        /// <param name="_areaCode"></param>
        /// <returns></returns>
        public bool ExistAreaModel(string _areaCode)
        {
            string strWhere = "AreaCode = '" + _areaCode + "'";
            return BindEntity<AbstractArea>.CreateInstanceDAL(oleDb, WorkId).Exists(strWhere);
        }

        public int AddArea(AbstractArea areaInfo)
        {
            int ret = 0;
            ret = BindEntity<AbstractArea>.CreateInstanceDAL(oleDb, WorkId).Add(areaInfo);
            return ret;
        }

        public int UpdateArea(AbstractArea areaInfo)
        {
            int ret = 0;
            ret = BindEntity<AbstractArea>.CreateInstanceDAL(oleDb, WorkId).Update(areaInfo);
            return ret;
        }
        public bool DeleteArea(string _areaCode)
        {
            string strWhere = "AreaCode = '" + _areaCode + "'";
            return BindEntity<AbstractArea>.CreateInstanceDAL(oleDb, WorkId).Delete(strWhere)>0;
        }

        public List<AbstractArea> GetAreaList()
        {
            string strWhere = "";
            return BindEntity<AbstractArea>.CreateInstanceDAL(oleDb, WorkId).GetListArray(strWhere);
        }

        public DataTable GetAreaFieldList()
        {
            string strWhere = "select AreaCode,AreaName from wy_area";
            return  oleDb.GetDataTable(strWhere);
        }

        /// <summary>
        /// 是否存在该大楼对象
        /// </summary>
        /// <param name="_buildingcode"></param>
        /// <returns></returns>
        public bool ExistBuildingModel(string _buildingcode)
        {
            string strWhere = "BuildingCode = '" + _buildingcode + "'";
            return BindEntity<AbstractBuilding>.CreateInstanceDAL(oleDb, WorkId).Exists(strWhere);
        }

        public int AddBuilding(AbstractBuilding buildingInfo)
        {
            int ret = 0;
            ret = BindEntity<AbstractBuilding>.CreateInstanceDAL(oleDb, WorkId).Add(buildingInfo);
            return ret;
        }

        public int UpdateBuilding(AbstractBuilding buildingInfo)
        {
            int ret = 0;
            ret = BindEntity<AbstractBuilding>.CreateInstanceDAL(oleDb, WorkId).Update(buildingInfo);
            return ret;
        }
        public bool DeleteBuilding(string _buildingcode)
        {
            string strWhere = "BuildingCode = '" + _buildingcode + "'";
            return BindEntity<AbstractBuilding>.CreateInstanceDAL(oleDb, WorkId).Delete(strWhere) > 0;
        }

        public List<AbstractBuilding> GetBuildingList(string _areaCode)
        {
            string strWhere = "";
            if (_areaCode != "")
            {
                strWhere = "AreaCode = '" + _areaCode + "'";
            }
           
            return BindEntity<AbstractBuilding>.CreateInstanceDAL(oleDb, WorkId).GetListArray(strWhere);
        }


        public DataTable GetBuildingFieldList(string _areaCode)
        {
            string strWhere = "select BuildingCode,BuildingName,FloorNum from WY_Building";
            if(_areaCode!="")
                strWhere+= "  where AreaCode='" + _areaCode+"'";
            return oleDb.GetDataTable(strWhere);
        }

        /// <summary>
        /// 是否存在该房间对象
        /// </summary>
        /// <param name="_roomId"></param>
        /// <returns></returns>
        public bool ExistRoomModel(string _roomNumber)
        {
            string strWhere = "RoomNumber = '" + _roomNumber+"'";
            return BindEntity<AbstractRoom>.CreateInstanceDAL(oleDb, WorkId).Exists(strWhere);
        }

        public int AddRoom(AbstractRoom roomInfo)
        {
            int ret = 0;
            ret = BindEntity<AbstractRoom>.CreateInstanceDAL(oleDb, WorkId).Add(roomInfo);
            return ret;
        }

        public int UpdateRoom(AbstractRoom roomInfo)
        {
            int ret = 0;
            ret = BindEntity<AbstractRoom>.CreateInstanceDAL(oleDb, WorkId).Update(roomInfo);
            return ret;
        }
        public bool DeleteRoom(string _roomId)
        {
            string strWhere = "RoomId = '" + _roomId + "'";
            return BindEntity<AbstractRoom>.CreateInstanceDAL(oleDb, WorkId).Delete(strWhere) > 0;
        }

        public DataTable GetRoomList(string _areacode, string _buildingCode, string _useType,string _useStatus, string _roomname, PageInfo page)
        {

            string strsql = @"select a.*,i.AreaName,j.BuildingName,c.OwnerId as PersonId,c.POwnerId,c.RelationType,c.Name,c.Sex,c.BirthdayDate,c.HomeTown, " +
                            " c.CardType,c.CardNum,c.OftenRelaAdress,c.Fax,c.QQ,c.Telephone,c.OftenMobile,c.Mail,c.Pym,c.Wbm,c.Memo as PerMemo,d.TypeName as UseTypeName," +
                            " e.TypeName as  RoomModelName,f.TypeName as TowardName,g.TypeName as StructName,h.TypeName as UseStatusName " +
                            " from  WY_Room a left join WY_RoomOwnerRelation b on a.RoomId=b.RoomId " +
                            " left join WY_Owner c  on b.OwnerId=c.OwnerId " +
                            " left join WY_BaseDic d  on a.UseType=d.Code and d.standtype=1 " +
                            " left join WY_BaseDic e  on  a.RoomModel=e.Code and e.standtype=2 " +
                            " left join WY_BaseDic f  on a.Toward=f.Code and f.standtype=3" +
                            " left join WY_BaseDic g  on a.Struct=g.Code and g.standtype=4 " +
                            " left join WY_BaseDic h  on a.UseStatus=h.Code and h.standtype=5 " +
                            " left join WY_Area i  on a.AreaCode=i.AreaCode " +
                            " left join WY_Building j  on a.BuildingCode=j.BuildingCode " +
                            "  where  (a.DeleteFlag = 0) AND (a.AreaCode like '%{0}%') AND (a.BuildingCode like '%{1}%') And (a.UseType in {2}) And (a.UseStatus in {3}) And (a.RoomNumber like '%{4}%' or c.Name like '%{4}%') " +
                            "   ORDER BY a.RoomNumber";
            page.KeyName = "RoomNumber";
            strsql = string.Format(strsql, _areacode, _buildingCode, _useType, _useStatus,_roomname);
            strsql = CoreFrame.Common.FilterString.FormatSql(strsql, page, oleDb);
            return oleDb.GetDataTable(strsql);
        }

        public AbstractRoom GetRoom(string _roomNumber)
        {
            string strWhere = "RoomNumber = '" + _roomNumber + "'";
            return BindEntity<AbstractRoom>.CreateInstanceDAL(oleDb, WorkId).GetModel(strWhere);
        }

        public AbstractBuilding GetBuilding(string _buildingCode)
        {
            return BindEntity<AbstractBuilding>.CreateInstanceDAL(oleDb, WorkId).GetModel("BuildingCode='"+_buildingCode+"'");
        }
        public bool JoinRoom(string srccId)
        {
            string strsql = @"update WY_RoomOwnerRelation set JoinFlag=1,JoinDate='" + DateTime.Now.Date + "' where RoomId='"+srccId+"'";
            return oleDb.DoCommand(strsql) > 0;
        }
        public bool CancelJoinRoom(string srccId)
        {
            string strsql = @"update WY_RoomOwnerRelation set JoinFlag=0 where RoomId='" + srccId + "'";
            return oleDb.DoCommand(strsql) > 0;
        }

        public bool JoinShop(string srccId)
        {
            string strsql = @"update WY_Shop set JoinFlag=1,JoinDate='" + DateTime.Now.Date + "' where ShopId='" + srccId + "'";
            return oleDb.DoCommand(strsql) > 0;
        }
        public bool CancelJoinShop(string srccId)
        {
            string strsql = @"update WY_Shop set JoinFlag=0 where ShopId='" + srccId + "'";
            return oleDb.DoCommand(strsql) > 0;
        }

        public bool CancelJoinCustomer(string srccId)
        {
            string strsql = @"update WY_Customer set JoinFlag=0,DeleteFlag=1 where CustomerId='" + srccId + "'";
            return oleDb.DoCommand(strsql) > 0;
        }

        public int AddShop(AbstractShop shopInfo, List<string>  _roomIdList, List<string> _roomNumberList)
        {
            int ret = 0;
            ret = BindEntity<AbstractShop>.CreateInstanceDAL(oleDb, WorkId).Add(shopInfo);
            if(ret<=0)
            {
                return ret;
            }
            for (int i = 0; i < _roomIdList.Count;i++ )
            {
                string strSql = "insert into WY_ShopRoomRelation(ShopId,RoomId,RoomNumber) values('" + shopInfo.ShopId + "','" + _roomIdList[i] + "','" + _roomNumberList[i] + "')";
                ret = oleDb.DoCommand(strSql);
            }

            return ret;
        }

        public List<AbstractRoom> GetShopsRoomList(string _shopId)
        {
            string strWhere = "select RoomId from WY_ShopRoomRelation where ShopId='" + _shopId + "'";
            DataTable dtRoom = oleDb.GetDataTable(strWhere);
            string strroom="";
            if (dtRoom.Rows.Count == 0)
                return null;
            foreach(DataRow dr in dtRoom.Rows)
            {
                strroom = strroom +"'"+ dr["RoomId"].ToString() + "',";
            }
            strroom = strroom.Remove(strroom.LastIndexOf(",")).Trim();
            return BindEntity<AbstractRoom>.CreateInstanceDAL(oleDb, WorkId).GetListArray("RoomId in (" + strroom + ")");
        }

        public int UpdateShop(AbstractShop shopInfo, List<string> _roomIdList, List<string> _roomNumberList)
        {
            int ret = 0;
            string strSql = "";
            ret = BindEntity<AbstractShop>.CreateInstanceDAL(oleDb, WorkId).Update(shopInfo);
            if (ret <= 0)
            {
                return ret;
            }
            strSql = "delete from WY_ShopRoomRelation where ShopId='" + shopInfo.ShopId + "'";
            ret = oleDb.DoCommand(strSql);
            if (ret <= 0)
            {
                return ret;
            }
            for (int i = 0; i < _roomIdList.Count; i++)
            {
                strSql = "insert into WY_ShopRoomRelation(ShopId,RoomId,RoomNumber) values('" + shopInfo.ShopId + "','" + _roomIdList[i] + "','" + _roomNumberList[i] + "')";
                ret = oleDb.DoCommand(strSql);
            }

            return ret;
        }

        public bool DeleteShop(string _shopid)
        {
            string strWhere = "ShopId = '" + _shopid + "'";

            string strSql = "delete from WY_ShopRoomRelation where " + strWhere;
            int  ret = oleDb.DoCommand(strSql);
            if (ret <= 0)
            {
                return false;
            }

            return BindEntity<AbstractShop>.CreateInstanceDAL(oleDb, WorkId).Delete(strWhere) > 0;
        }

        public DataTable GetShopAndOwnerList(string shopname, PageInfo page)
        {
            string strsql = @"SELECT     ShopId as RoomId , ShopName, UseArea,A.OwnerId as PersonId, OwnerName,B.*
                                FROM         WY_Shop A 
                                LEFT OUTER JOIN  WY_Owner B ON A.OwnerId=B.OwnerId
                                WHERE     (A.DeleteFlag = 0) AND (ShopName LIKE '%{0}%')";
            page.KeyName = "RoomId";
            strsql = string.Format(strsql, shopname);
            strsql = CoreFrame.Common.FilterString.FormatSql(strsql, page, oleDb);
            return oleDb.GetDataTable(strsql);
        }
    }
}
